package com.handpay.zztong.hp.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.handpay.framework.utils.BitmapLoaderNew;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.bean.Banner;
import com.handpay.zztong.hp.ui.AdGalleryHelper.OnGallerySwitchListener;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 无限滚动广告栏组件 可以作为对外的布局引用
 */
@SuppressWarnings("deprecation")
public class AdGallery extends Gallery implements
		android.widget.AdapterView.OnItemClickListener,
		android.widget.AdapterView.OnItemSelectedListener, OnTouchListener {

	public boolean isAutoSwitch = true; // 是否自动切换
	private Context mContext;
	private long mSwitchTime; // 图片切换时间
	private boolean isscorlling = true;// 控制是否能滑动
	private boolean runflag = false;
	private Timer mTimer;// 自动滚动的定时器

	private OnAdItemClickListener onItemClick;

	private OnGallerySwitchListener mGallerySwitchListener;

	private Banner[] mAds;
	private int height = 0;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			int position = getSelectedItemPosition();
			if (position >= (getCount() - 1)) {
				setSelection(getCount() / 2, true); // 跳转到第二张图片，在向左滑动一张就到了第一张图片
				onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
			} else {
				onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
			}
		}
	};

	public AdGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		mTimer = new Timer();
	}

	public AdGallery(Context context) {
		super(context);
		this.mContext = context;
		mTimer = new Timer();
	}

	public AdGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		mTimer = new Timer();
	}

	/**
	 * 对控件的初始化参数
	 * 
	 * @param ads
	 *            广告组
	 * @param switchTime
	 *            自动切换间隔时间
	 * @param gallerySwitchListener
	 *            切换事件的监听接口
	 * @param isAutoSwitch
	 *            是否需要自动切换
	 */
	public void init(Banner[] ads, long switchTime,
			OnGallerySwitchListener gallerySwitchListener,
			boolean isAutoSwitch, int height) {
		this.mSwitchTime = switchTime;
		this.mGallerySwitchListener = gallerySwitchListener;
		this.isAutoSwitch = isAutoSwitch;
		this.mAds = ads;
		setAdapter(new AdAdapter());
		this.setOnItemClickListener(this);
		this.setOnTouchListener(this);
		this.setOnItemSelectedListener(this);
		this.setSoundEffectsEnabled(false);
		// setSpacing(10); //不能包含spacing，否则会导致onKeyDown()失效!!!
		setSelection(0); // 默认选中中间位置为起始位置
		setFocusableInTouchMode(true);
		setAutoSrcoll();
		this.height = height;
	}

	/**
	 * 设置点击事件的接口
	 * 
	 * @param onItemClick
	 */
	public void setAdOnItemClickListener(OnAdItemClickListener onItemClick) {
		this.onItemClick = onItemClick;
	}

	/**
	 * 初始化时判断是否需要设置自动切换广告图
	 */
	private void setAutoSrcoll() {
		if (isAutoSwitch) {
			setRunFlag(true);
			startAutoScroll();
		}
	}

	class ViewHolder {
		ImageView imageview;
	}

	class AdAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mAds.length * (Integer.MAX_VALUE / mAds.length);
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.adgallery_image, null);
				@SuppressWarnings("deprecation")
				Gallery.LayoutParams params = new Gallery.LayoutParams(
						Gallery.LayoutParams.FILL_PARENT, height / 4);
				convertView.setLayoutParams(params);

				viewHolder = new ViewHolder();
				viewHolder.imageview = (ImageView) convertView
						.findViewById(R.id.gallery_image);
				viewHolder.imageview
						.setLayoutParams(new RelativeLayout.LayoutParams(
								LayoutParams.FILL_PARENT, height / 4));
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			// 使用FinalBitmap显示图片
			/*
			 * FinalBitmap.create(mContext).display(viewHolder.imageview,
			 * mAds[position % mAds.length].getCoverUrl(),
			 * viewHolder.imageview.getWidth(),
			 * viewHolder.imageview.getHeight(),null, null);
			 */
			// BitmapLoader.getInstance(mContext).asynLoadBitmap(mAds[position %
			// mAds.length].getCoverUrl(),
			// mAds[position % mAds.length].getCoverUrl(),
			// new MyImageCallback(viewHolder.imageview));

			BitmapLoaderNew.getInstance(mContext).loadImage(
					mAds[position % mAds.length].getCoverUrl(),
					viewHolder.imageview);
			return convertView;
		}
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int kEvent;
		if (isScrollingLeft(e1, e2)) { // 检查是否往左滑动
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else { // 检查是否往右滑动
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}

		onKeyDown(kEvent, null);
		return true;

	}

	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > (e1.getX() + 50);
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		//
		return super.onScroll(e1, e2, distanceX, distanceY);
	}

	/**
	 * 开始自动滚动
	 */
	public void startAutoScroll() {
		mTimer.schedule(new TimerTask() {

			public void run() {
				if (runflag) {
					Message msg = new Message();
					if (getSelectedItemPosition() < (getCount() - 1)) {
						msg.what = getSelectedItemPosition() + 1;
					} else {
						msg.what = 0;
					}
					handler.sendMessage(msg);
				}
			}
		}, mSwitchTime, mSwitchTime);

	}

	public void setRunFlag(boolean flag) {
		runflag = flag;
	}

	public boolean isAutoScrolling() {
		return mTimer != null;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {

		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (mAds.length > 1) {
			if (MotionEvent.ACTION_UP == event.getAction()
					|| MotionEvent.ACTION_CANCEL == event.getAction()) {
				// 重置自动滚动任务
				setRunFlag(true);
			} else {
				// 停止自动滚动任务
				setRunFlag(false);
			}
		}
		Log.i("result", "onTouch");
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (onItemClick != null && mAds.length == 1) {
				Log.i("result", "onItemClick");
				onItemClick.setItemClick(getPositionForView(v) % mAds.length);
			}
		}
		return isscorlling;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		mGallerySwitchListener.onGallerySwitch(position % (mAds.length));
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Log.i("result", "onItemClick-------->");
		if (onItemClick != null) {
			onItemClick.setItemClick(position % mAds.length);
		} else {
			Log.v("AdGallery", "hasn't set up 'OnAdItemClickListener'");
		}
	}

	/***
	 * 设置是否能滑动
	 * 
	 * @param isscorlling
	 */
	public void setIsCanScorlling(boolean isscorlling) {
		this.isscorlling = isscorlling;
	}

	/**
	 * 当点击了AdGallery的图片时触发并回调接口中的方法 　
	 */
	public interface OnAdItemClickListener {
		void setItemClick(int position);
	}
}
